package com.artqiyi.dollmachine.utils.manager;


import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseApp;

import java.io.IOException;

import timber.log.Timber;

public class MediaManager {

    private MediaPlayer mPlayer;

    private boolean isPause;

//    public void init() {
//        if (mPlayer == null) {
//            mPlayer = MediaPlayer.create(BaseApp.getInstance(), R.raw.music_bg);
//        }
//    }

    public MediaManager() {
        if (mPlayer == null) {
            mPlayer = MediaPlayer.create(BaseApp.getInstance(), R.raw.music_bg);
        }
    }

    public void start() {
        try {
            if (mPlayer.isPlaying()) {
                mPlayer.stop();
                mPlayer.release();
                mPlayer = MediaPlayer.create(BaseApp.getInstance(), R.raw.music_bg);
            }
            mPlayer.start();
            mPlayer.setLooping(true);                   //循环播放
//            if (null != mPlayer) {
//                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//                mPlayer.setOnErrorListener(onErrorListener);
//                mPlayer.setOnCompletionListener(this);
//                mPlayer.prepare();
//                mPlayer.start();
//                mPlayer.setLooping(true);                   //循环播放
//            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
            Timber.d("IllegalState:" + e.toString());
            mPlayer.stop();
            mPlayer = null;
            mPlayer = new MediaPlayer();
        }
    }

//    public void start(Context context, OnErrorListener onErrorListener) {
//        if (mPlayer == null) {
//            mPlayer = MediaPlayer.create(context, R.raw.music);
////            mPlayer = new MediaPlayer();
//            //保险起见，设置报错监听
//            mPlayer.setOnErrorListener(onErrorListener);
//        } else {
//            mPlayer.reset();
//        }
//
//        try {
//            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//            mPlayer.setOnCompletionListener(this);
////            File file = new File(filePathString);
////            FileInputStream fis = new FileInputStream(file);
////            mPlayer.setDataSource(fis.getFD());
////			mPlayer.setDataSource(path);
//            mPlayer.prepare();
//            mPlayer.start();
//        } catch (IllegalArgumentException e) {
//            e.printStackTrace();
//            Timber.d("IllegalArgument:" + e.toString());
//        } catch (SecurityException e) {
//            e.printStackTrace();
//            Timber.d("Security:" + e.toString());
//        } catch (IllegalStateException e) {
//            e.printStackTrace();
//            Timber.d("IllegalState:" + e.toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//            Timber.d("IOException:" + e.toString());
//            release();
//            onErrorListener.onError(null, 0, 0);
//        }
//    }

//    private MediaPlayer.OnErrorListener onErrorListener = new MediaPlayer.OnErrorListener() {
//
//        @Override
//        public boolean onError(MediaPlayer mp, int what, int extra) {
//            mPlayer.reset();
//            return false;
//        }
//    };

    //停止
    public void pause() {
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.pause();
            isPause = true;
        }
    }

    //继续
    public void resume() {
        if (mPlayer != null && isPause) {
            mPlayer.start();
            isPause = false;
        }
    }

    public void release() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

//    public static VoiceListener mVoiceListener;
//
//    public static void setVoiceListener(VoiceListener voiceListener) {
//        mVoiceListener = voiceListener;
//    }

//    public interface VoiceListener {
//        void cancelVoiceAnimation();
//    }

}
